package cn.aresoft.web.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import cn.aresoft.framework.support.action.BaseActionSupport;
import cn.aresoft.logic.service.face.custom.IBasicUserInfoService;
import cn.aresoft.model.BasicUserAvatar;
import cn.aresoft.model.BasicUserInfo;

public class VipInfoAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;
	/**
	 * 获取页面传来的所有参数
	 */
	private String getUrlParam(HttpServletRequest request, String name) {
		return request.getParameter(name);
	}
	private BasicUserInfo getUserInfo(HttpServletRequest request) {
		//return (BasicUserInfo)request.getAttribute(WebConstants.USER_SESSION_KEY);
		BasicUserInfo b=new BasicUserInfo();
		b.setUserid(1523);
		return b;
	}
	public String index(){
		return "index";
	}
	public String info(){
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		userInfo = new BasicUserInfo();
		userInfo.setUsername("颜丽");
		userInfo.setPassword("700711");
		userInfo = (BasicUserInfo) iBasicUserInfoService.load("load", userInfo);
		
		return "info";
	}
	/**
	 * 修改会员信息
	 * @return
	 * @throws Exception
	 */
	public String updateVipInfo() throws Exception{
		Map<String,Object> param = new HashMap<String,Object>();
		if(userInfo==null){
			userInfo=new BasicUserInfo();
		}
		userInfo.setSex(getUrlParam(request, "sex"));
		userInfo.setProvince(getUrlParam(request, "province"));
		userInfo.setCity(getUrlParam(request, "city"));
		userInfo.setBirthd(getUrlParam(request, "birthd"));
		userInfo.setBlog(getUrlParam(request, "blog"));
		userInfo.setEmail(getUrlParam(request, "email"));
		userInfo.setQq(getUrlParam(request, "qq"));
		userInfo.setAddress(getUrlParam(request, "address"));
		userInfo.setUserid(this.getUserInfo(request).getUserid());
		userInfo.setVippassword(getUrlParam(request, "vippassword"));
		
		Object idtype=getUrlParam(request, "idType");
		if(idtype!=null){
			userInfo.setIdType(Integer.parseInt(idtype.toString()));
		}
		userInfo.setIdenficationcard(getUrlParam(request, "idenficationcard"));
		PrintWriter pw = response.getWriter();
		try{
			iBasicUserInfoService.insert("updateBasicUserInfo", userInfo);
			pw.print("ok");
		}catch(Exception e){
			pw.print("error");
			e.printStackTrace();
		}
		return null;
	}
	//修改密码
	public String updateP(){
		return "updatePassword";
	}
	//操作修改密码
	public String todoUpdateP() throws Exception{
		if(userInfo==null){
			userInfo=new BasicUserInfo();
		}
		userInfo.setUserid(this.getUserInfo(request).getUserid());
		userInfo.setOldvippassword(getUrlParam(request, "oldvippassword"));
		userInfo.setVippassword(getUrlParam(request, "vippassword"));
		
		PrintWriter pw = response.getWriter();
		try{
			Map ret=(Map)iBasicUserInfoService.getObject("validatePassword", userInfo);
			if(ret==null){
				pw.print("mobileError");
				return null;
			}
			iBasicUserInfoService.insert("updateBasicUserInfo", userInfo);
			pw.print("ok");
		}catch(Exception e){
			pw.print("error");
			e.printStackTrace();
		}
		return null;
	}
	//修改头像
	public String updateAvatar(){
		return "updateAvatar";
	}
	//显示头像
	public String getUserAvatar() throws ServletException, SQLException,
			IOException {
		Map param = new HashMap();
		param.put("userId", this.getUserInfo(request).getUserid());
		Object userAvavtar = iBasicUserInfoService.getObjectInfo("getUserAvatar", param);
		BasicUserAvatar userPicInfo;
		if (userAvavtar != null) {
			userPicInfo = (BasicUserAvatar) userAvavtar;
			if (userPicInfo.getUserphoto().length > 0) {
				InputStream in = new ByteArrayInputStream(userPicInfo.getUserphoto());
				response.reset();
				response.setContentType("image/jpeg");
				OutputStream os = response.getOutputStream();
				byte[] b = new byte[1024];
				int len;
				while ((len = in.read(b, 0, b.length)) > 0) {
					os.write(b, 0, len);
				}
				os.flush();
				in.close();
				os.close();
			}
		}
		return null;
	}
	
	private IBasicUserInfoService iBasicUserInfoService;
	private BasicUserInfo userInfo;
	private String goingToURL; 

	public void setGoingToURL(String goingToURL) {
		this.goingToURL = goingToURL;
	}

	public BasicUserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(BasicUserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public void setIBasicUserInfoService(IBasicUserInfoService basicUserInfoService) {
		iBasicUserInfoService = basicUserInfoService;
	}
}	